//
//  StartGame.cpp
//  Game2048
//
//  Created by fengyi on 15/8/20.
//
//

#include "StartGame.h"
#include "cocos-ext.h"
#define MOVE "move.mp3"
#define DOUBLE "double.mp3"
#define GAMEOVER "gameover.mp3"


USING_NS_CC_EXT;
unsigned int StartGame::score = 0;
Scene* StartGame::createScene()
{
    auto scene = Scene::create();
    auto layer = StartGame::create();
    scene->addChild(layer);
    return scene;
}

bool StartGame::init()
{
    if (!Layer::init())
    {
        return false;
    }
    Size size = Director::getInstance()->getVisibleSize();
    //白色背景
    auto bg_white = LayerColor::create(Color4B::WHITE,size.width,size.height);
    this->addChild(bg_white);
    //预加载音效
    auto audio = SimpleAudioEngine::getInstance();
    audio->preloadEffect(MOVE);
    audio->preloadEffect(DOUBLE);
    audio->preloadEffect(GAMEOVER);
    audio->setEffectsVolume(0.5);
    //音效开关
    auto item_on = MenuItemImage::create("sound_on.png","sound_on.png",CC_CALLBACK_0(StartGame::setAudioEfx, this));
    auto item_off = MenuItemImage::create("sound_off.png","sound_off.png",CC_CALLBACK_0(StartGame::setAudioEfx, this));
    item_on->setScale(0.8f);
    item_off->setScale(0.8f);
    auto item = MenuItemToggle::createWithCallback(CC_CALLBACK_0(StartGame::setAudioEfx, this),item_on,item_off,nullptr);
    auto audio_menu = Menu::create(item,nullptr);
    audio_menu->setPosition(Point(size.width - 50,size.height - 50));    
    addChild(audio_menu);
    //重新开始游戏按钮
    auto restart_label = Label::createWithBMFont("fonts/futura-48.fnt", "RESTART");
    auto restart_item = MenuItemLabel::create(restart_label,CC_CALLBACK_0(StartGame::restart_callback, this));
    auto restart_menu = Menu::create(restart_item,nullptr);
    restart_menu->setPosition(Point(size.width - 100, size.height - 100));
    addChild(restart_menu);
    //游戏名称
    auto game_name = Label::createWithBMFont("fonts/futura-48.fnt", "2048");
    game_name->setScale(1.5f);
    game_name->setPosition(Point(size.width/2,size.height-game_name->getContentSize().height/2*1.5));
    addChild(game_name);
    //分数
    score_label = Label::createWithSystemFont(StringUtils::format("%d", score), "", 40);
    score_label->setPosition(Point(100,size.height - 100));
    score_label->setColor(Color3B::BLACK);
    addChild(score_label);
    
    //初始化二维数组
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL; c++)
        {
            m_map[r][c] = 0;
        }
    }
    //添加4x4背景
    bg = LayerColor::create(colorbg, TILE_WIDTH*COL+SPACE*(COL+1),  TILE_WIDTH*ROW+SPACE*(ROW+1));
    bg->setPosition(Point((size.width - bg->getContentSize().width)/2,
                          (size.height -  bg->getContentSize().height)/2));
    addChild(bg);
    
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL; c++)
        {
            auto black_num = LayerColor::create(color0, TILE_WIDTH, TILE_WIDTH);
            black_num->setPosition(Point(TILE_WIDTH * c + SPACE*(c + 1),
                               TILE_WIDTH * r + SPACE*(r + 1)));
            bg->addChild(black_num);
        }
    }
    
    //初始化一个2的数字块
    auto tile = NumTile::create();
    int num = rand()%16;
    tile->m_number = 2;
    tile->showAt(num/COL, num%COL);
    bg->addChild(tile);
    m_vec.pushBack(tile);
    m_map[num/COL][num%COL] = m_vec.getIndex(tile)+1;
    
    //返回键处理
    isDoubleClick = false;
    auto listenerKeyBoard = EventListenerKeyboard::create();
    listenerKeyBoard->onKeyReleased = [&](EventKeyboard::KeyCode keyCode, Event* event)
    {
        if (keyCode == EventKeyboard::KeyCode::KEY_BACK)
        {
            if (isDoubleClick)
            {
               stopGame(); 
            }
            else
            {
                isDoubleClick = true;
                scheduleOnce(schedule_selector(StartGame::doubleClickCallBack),0.3);
            }
        }
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyBoard, this);
    
    //触摸处理
    auto event = EventListenerTouchOneByOne::create();
    event->onTouchBegan = [&](Touch* touch,Event* event)
    {
        m_x = touch->getLocation().x;
        m_y = touch->getLocation().y;
        b_startMover = true;
        return true;
    };
    event->onTouchMoved = [&](Touch* touch,Event* event)
    {
        int x = touch->getLocation().x;
        int y = touch->getLocation().y;
        if (b_startMover &&(abs(m_x - x)>10||abs(m_y - y)>10))
        {
            b_startMover = false;
            Move_Dir dir;
            if (abs(m_y - y) > abs(m_x - x)) 
            {
                if (m_y < y) 
                {
                    dir = Move_Dir::UP;
                }
                else
                {
                    dir = Move_Dir::DOWN;
                }
            }
            else
            {
                if (m_x > x)
                {
                    dir = Move_Dir::LEFT;
                }
                else
                {
                    dir = Move_Dir::RIGHT;
                }
            }
            moveAllTile(dir);
        }
        
    };

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(event, this);
    
    
    return true;
}

void StartGame::doubleClickCallBack(float dt)
{
    isDoubleClick = false;
}

void StartGame::setAudioEfx()
{
    float volume = SimpleAudioEngine::getInstance()->getEffectsVolume();
    if (volume == 0) 
    {
        SimpleAudioEngine::getInstance()->setEffectsVolume(0.5f);
    }
    else
    {
        SimpleAudioEngine::getInstance()->setEffectsVolume(0.0f);
    }
}
void StartGame::restart_callback()
{
    Director::getInstance()->getEventDispatcher()->pauseEventListenersForTarget(this->getParent(),true);
    
    
    Size size = Director::getInstance()->getWinSize();
    auto layer_bg = LayerColor::create(Color4B::WHITE, size.width*4/5, 200);
    layer_bg->setPosition(size/2 - layer_bg->getContentSize()/2);
    addChild(layer_bg);
    auto restart_label = Label::createWithBMFont("fonts/futura-48.fnt", "OK");
    auto restart_item = MenuItemLabel::create(restart_label, CC_CALLBACK_0(StartGame::restart_ok, this));
    auto cancel_label = Label::createWithBMFont("fonts/futura-48.fnt", "CANCEL");
    auto cancel_item = MenuItemLabel::create(cancel_label,CC_CALLBACK_0(StartGame::restart_cancel, this));
    auto menu = Menu::create(restart_item,cancel_item,nullptr);
    menu->alignItemsHorizontallyWithPadding(40);
    menu->setPosition(layer_bg->getContentSize()/2);
    layer_bg->addChild(menu);
    layer_bg->setTag(1000);
    layer_bg->setScale(0.1f);
    layer_bg->runAction(Sequence::create(ScaleTo::create(0.1f, 0.5f),
                                         ScaleTo::create(0.1f, 1.1f),
                                         ScaleTo::create(0.1f, 1.0f),
                                         NULL));
}

void StartGame::restart_cancel()
{
    this->getChildByTag(1000)->runAction(Sequence::create(ScaleTo::create(0.1f, 0.5f),
                                                          ScaleTo::create(0.1f, 0.01f),
                                                          RemoveSelf::create(),                                                          NULL));
    Director::getInstance()->getEventDispatcher()->resumeEventListenersForTarget(this->getParent(),true);
}

void StartGame::restart_ok()
{
    restart();
    restart_cancel();
}

void StartGame::moveAllTile(Move_Dir dir)
{
    isMoved = false;
    isEliminate = false;
    //移动并消除块
    switch (dir) 
    {
        case Move_Dir::UP:
            moveUp();
            break;
        case Move_Dir::DOWN:
            moveDown();
            break;
        case Move_Dir::LEFT:
            moveLeft();
            break;
        case Move_Dir::RIGHT:
            moveRight();
            break;
        default:
            break;
    }
    if (!isEliminate && isMoved)
    {
         SimpleAudioEngine::getInstance()->playEffect(MOVE);
    }
    else if(isEliminate)
    {
        SimpleAudioEngine::getInstance()->playEffect(DOUBLE);
    }
    //产生新块
    if (isMoved || isEliminate) 
    {
        scheduleOnce(schedule_selector(StartGame::newTile),0.3f);
    }
    
    if (m_vec.size() == ROW*COL && !canMove()) 
    {
        SimpleAudioEngine::getInstance()->playEffect(GAMEOVER);
        //restart();
    }
}

void StartGame::moveUp()
{
    //方案1有缺陷，移动时产生的新块有时会继续消除
    /*
    for (int c = 0; c < COL; ++c)
    {
        for (int r = ROW - 1; r >= 0; --r)
        {
            if (m_map[r][c] > 0) 
            {
                for (int r1 = r; r1 < ROW - 1; ++r1) 
                {
                    if (m_map[r1 + 1][c] == 0) 
                    {
                        m_map[r1 + 1][c] = m_map[r1][c];
                        m_map[r1][c] = 0;
                        m_vec.at(m_map[r1 + 1][c] - 1)->moveTo(r1 + 1, c);
                        isMoved = true;
                    }
                    else//消除块
                    {
                        int numObj =m_vec.at(m_map[r1][c] - 1)->m_number;
                        int numNow =m_vec.at(m_map[r1 + 1][c] - 1)->m_number;
                        if (numObj == numNow)
                        {
                            int index = m_map[r1][c];
                            m_vec.at(m_map[r1 + 1][c] - 1)->doubleNum();
                            score += m_vec.at(m_map[r1][c] - 1)->m_number;
                            score_label->setString(StringUtils::format("%d",score));
                            m_vec.at(m_map[r1][c] - 1)->removeFromParent();
                            m_vec.erase(m_map[r1][c]-1);
                            for (int row = 0; row < ROW; ++row)
                            {
                                for (int col = 0; col < COL; ++col)
                                {
                                    if(m_map[row][col] > index)
                                    {
                                        m_map[row][col]--;
                                    }
                                }
                            }
                            m_map[r1][c] = 0;
                            isEliminate = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    */
    for (int c = 0; c < COL; ++c)//移动块
    {
        for (int r = ROW - 1; r >= 0; --r)
        {
            if (m_map[r][c] > 0)
            {
                for (int r1 = ROW - 1; r1 > r; --r1)
                {
                    if (m_map[r1][c] == 0)
                    {
                        m_map[r1][c] = m_map[r][c];
                        m_map[r][c] = 0;
                        m_vec.at(m_map[r1][c] - 1)->moveTo(r1, c);
                        isMoved = true;
                        break;
                    }
                }
            }
        }
    }
    for (int c = 0; c < COL; ++c) //消除块
    {
        for (int r = ROW - 1; r > 0; --r)
        {
            if (m_map[r][c] > 0 && m_map[r - 1][c] > 0)
            {
                int numObj =m_vec.at(m_map[r][c] - 1)->m_number;
                int numNow =m_vec.at(m_map[r - 1][c] - 1)->m_number;
                if (numObj == numNow)
                {
                    int index = m_map[r - 1][c];
                    m_vec.at(m_map[r][c] - 1)->doubleNum();
                    score += m_vec.at(m_map[r - 1][c] - 1)->m_number;
                    score_label->setString(StringUtils::format("%d",score));
                    m_vec.at(m_map[r - 1][c] - 1)->removeFromParent();
                    m_vec.erase(m_map[r - 1][c] - 1);
                    for (int row = 0; row < ROW; ++row)
                    {
                        for (int col = 0; col < COL; ++col)
                        {
                            if(m_map[row][col] > index)
                            {
                                m_map[row][col]--;
                            }
                        }
                    }
                    m_map[r - 1][c] = 0;
                    isEliminate = true;
                    for (int row = r - 2; row >= 0; --row) //消除块后再移动
                    {
                        if (m_map[row][c] > 0 && m_map[row + 1][c] == 0) 
                        {
                            m_map[row + 1][c] = m_map[row][c];
                            m_map[row][c] = 0;
                            m_vec.at(m_map[row + 1][c] - 1)->moveTo(row + 1, c);
                        }
                    }
                }
            }
        }
    }
    
}

void StartGame::moveDown()
{
    /*
    for (int c = 0; c < COL; ++c)
    {
        for (int r = 0; r < ROW; ++r)
        {
            if (m_map[r][c] > 0) 
            {
                for (int r1 = r; r1 > 0; --r1) 
                {
                    if (m_map[r1 - 1][c] == 0) 
                    {
                        m_map[r1 - 1][c] = m_map[r1][c];
                        m_map[r1][c] = 0;
                        m_vec.at(m_map[r1 - 1][c] - 1)->moveTo(r1 - 1, c);
                        isMoved = true;
                    }
                    else//消除块
                    {
                        int numObj =m_vec.at(m_map[r1][c] - 1)->m_number;
                        int numNow =m_vec.at(m_map[r1 - 1][c] - 1)->m_number;
                        if (numObj == numNow)
                        {
                            int index = m_map[r1][c];
                            m_vec.at(m_map[r1 - 1][c] - 1)->doubleNum();
                            score += m_vec.at(m_map[r1][c] - 1)->m_number;
                            score_label->setString(StringUtils::format("%d",score));
                            m_vec.at(m_map[r1][c] - 1)->removeFromParent();
                            m_vec.erase(m_map[r1][c]-1);
                            for (int row = 0; row < ROW; ++row)
                            {
                                for (int col = 0; col < COL; ++col)
                                {
                                    if(m_map[row][col] > index)
                                    {
                                        m_map[row][col]--;
                                    }
                                }
                            }
                            m_map[r1][c] = 0;
                            isEliminate = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    */
    for (int c = 0; c < COL; ++c) //移动块
    {
        for (int r = 0; r < ROW; ++r)
        {
            if (m_map[r][c] > 0)
            {
                for (int r1 = 0; r1 < r; ++r1)
                {
                    if (m_map[r1][c] == 0)
                    {
                        m_map[r1][c] = m_map[r][c];
                        m_map[r][c] = 0;
                        m_vec.at(m_map[r1][c] - 1)->moveTo(r1, c);
                        isMoved = true;
                        break;
                    }
                }
            }
        }
    }
    for (int c = 0; c < COL; ++c) //消除块
    {
        for (int r = 0; r < ROW - 1; ++r)
        {
            if (m_map[r][c] > 0 && m_map[r + 1][c] > 0)
            {
                int numObj =m_vec.at(m_map[r][c] - 1)->m_number;
                int numNow =m_vec.at(m_map[r + 1][c] - 1)->m_number;
                if (numObj == numNow)
                {
                    int index = m_map[r + 1][c];
                    m_vec.at(m_map[r][c] - 1)->doubleNum();
                    score += m_vec.at(m_map[r + 1][c] - 1)->m_number;
                    score_label->setString(StringUtils::format("%d",score));
                    m_vec.at(m_map[r + 1][c] - 1)->removeFromParent();
                    m_vec.erase(m_map[r + 1][c] - 1);
                    for (int row = 0; row < ROW; ++row)
                    {
                        for (int col = 0; col < COL; ++col)
                        {
                            if(m_map[row][col] > index)
                            {
                                m_map[row][col]--;
                            }
                        }
                    }
                    m_map[r + 1][c] = 0;
                    isEliminate = true;
                    for (int row = r + 2; row < ROW; ++row) //消除块后再移动
                    {
                        if (m_map[row][c] > 0 && m_map[row - 1][c] == 0) 
                        {
                            m_map[row - 1][c] = m_map[row][c];
                            m_map[row][c] = 0;
                            m_vec.at(m_map[row - 1][c] - 1)->moveTo(row - 1, c);
                        }
                    }
                }
            }
        }
    }
}

void StartGame::moveLeft()
{
    /*
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL; ++c)
        {
            if (m_map[r][c] > 0) 
            {
                for (int c1 = c; c1 > 0; --c1) 
                {
                    if (m_map[r][c1 - 1] == 0) 
                    {
                        m_map[r][c1 - 1] = m_map[r][c1];
                        m_map[r][c1] = 0;
                        m_vec.at(m_map[r][c1 - 1] - 1)->moveTo(r, c1 - 1);
                        isMoved = true;
                    }
                    else//消除块
                    {
                        int numObj =m_vec.at(m_map[r][c1] - 1)->m_number;
                        int numNow =m_vec.at(m_map[r][c1 - 1] - 1)->m_number;
                        if (numObj == numNow)
                        {
                            int index = m_map[r][c1];
                            m_vec.at(m_map[r][c1 - 1] - 1)->doubleNum();
                            score += m_vec.at(m_map[r][c1] - 1)->m_number;
                            score_label->setString(StringUtils::format("%d",score));
                            m_vec.at(m_map[r][c1] - 1)->removeFromParent();
                            m_vec.erase(m_map[r][c1] - 1);
                            for (int row = 0; row < ROW; ++row)
                            {
                                for (int col = 0; col < COL; ++col)
                                {
                                    if(m_map[row][col] > index)
                                    {
                                        m_map[row][col]--;
                                    }
                                }
                            }
                            m_map[r][c1] = 0;
                            isEliminate = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    */
    for (int r = 0; r < ROW; ++r) //移动块
    {
        for (int c = 0; c < COL; ++c)
        {
            if (m_map[r][c] > 0)
            {
                for (int c1 = 0; c1 < c; ++c1)
                {
                    if (m_map[r][c1] == 0)
                    {
                        m_map[r][c1] = m_map[r][c];
                        m_map[r][c] = 0;
                        m_vec.at(m_map[r][c1] - 1)->moveTo(r, c1);
                        isMoved = true;
                        break;
                    }
                }
            }
        }
    }
    for (int r = 0; r < ROW; ++r) //消除块
    {
        for (int c = 0; c < COL - 1; ++c)
        {
            if (m_map[r][c] > 0 && m_map[r][c + 1] > 0)
            {
                int numObj =m_vec.at(m_map[r][c] - 1)->m_number;
                int numNow =m_vec.at(m_map[r][c + 1] - 1)->m_number;
                if (numObj == numNow)
                {
                    int index = m_map[r][c + 1];
                    m_vec.at(m_map[r][c] - 1)->doubleNum();
                    score += m_vec.at(m_map[r][c + 1] - 1)->m_number;
                    score_label->setString(StringUtils::format("%d",score));
                    m_vec.at(m_map[r][c + 1] - 1)->removeFromParent();
                    m_vec.erase(m_map[r][c + 1] - 1);
                    for (int row = 0; row < ROW; ++row)
                    {
                        for (int col = 0; col < COL; ++col)
                        {
                            if(m_map[row][col] > index)
                            {
                                m_map[row][col]--;
                            }
                        }
                    }
                    m_map[r][c + 1] = 0;
                    isEliminate = true;
                    for (int col = c + 2; col < COL; ++col) //消除块后再移动
                    {
                        if (m_map[r][col] > 0 && m_map[r][col - 1] == 0) 
                        {
                            m_map[r][col - 1] = m_map[r][col];
                            m_map[r][col] = 0;
                            m_vec.at(m_map[r][col - 1] - 1)->moveTo(r, col - 1);
                        }
                    }
                }
            }
        }
    }
}


void StartGame::moveRight()
{
    /*
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = COL - 1; c >= 0; --c)
        {
            if (m_map[r][c] > 0) 
            {
                for (int c1 = c; c1 < COL - 1; ++c1) 
                {
                    if (m_map[r][c1 + 1] == 0) 
                    {
                        m_map[r][c1 + 1] = m_map[r][c1];
                        m_map[r][c1] = 0;
                        m_vec.at(m_map[r][c1 + 1] - 1)->moveTo(r, c1 + 1);
                        isMoved = true;
                    }
                    else//消除块
                    {
                        int numObj =m_vec.at(m_map[r][c1] - 1)->m_number;
                        int numNow =m_vec.at(m_map[r][c1 + 1] - 1)->m_number;
                        if (numObj == numNow)
                        {
                            int index = m_map[r][c1];
                            m_vec.at(m_map[r][c1 + 1] - 1)->doubleNum();
                            score += m_vec.at(m_map[r][c1] - 1)->m_number;
                            score_label->setString(StringUtils::format("%d",score));
                            m_vec.at(m_map[r][c1] - 1)->removeFromParent();
                            m_vec.erase(m_map[r][c1]-1);
                            for (int row = 0; row < ROW; ++row)
                            {
                                for (int col = 0; col < COL; ++col)
                                {
                                    if(m_map[row][col] > index)
                                    {
                                        m_map[row][col]--;
                                    }
                                }
                            }
                            m_map[r][c1] = 0;
                            isEliminate = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    */
    for (int r = 0; r < ROW; ++r) //移动块
    {
        for (int c = COL - 1; c >= 0; --c)
        {
            if (m_map[r][c] > 0)
            {
                for (int c1 = COL - 1; c1 > c; --c1)
                {
                    if (m_map[r][c1] == 0)
                    {
                        m_map[r][c1] = m_map[r][c];
                        m_map[r][c] = 0;
                        m_vec.at(m_map[r][c1] - 1)->moveTo(r, c1);
                        isMoved = true;
                        break;
                    }
                }
            }
        }
    }
    for (int r = 0; r < ROW; ++r) //消除块
    {
        for (int c = COL - 1; c > 0; --c)
        {
            if (m_map[r][c] > 0 && m_map[r][c - 1] > 0)
            {
                int numObj =m_vec.at(m_map[r][c] - 1)->m_number;
                int numNow =m_vec.at(m_map[r][c - 1] - 1)->m_number;
                if (numObj == numNow)
                {
                    int index = m_map[r][c - 1];
                    m_vec.at(m_map[r][c] - 1)->doubleNum();
                    score += m_vec.at(m_map[r][c - 1] - 1)->m_number;
                    score_label->setString(StringUtils::format("%d",score));
                    m_vec.at(m_map[r][c - 1] - 1)->removeFromParent();
                    m_vec.erase(m_map[r][c - 1] - 1);
                    for (int row = 0; row < ROW; ++row)
                    {
                        for (int col = 0; col < COL; ++col)
                        {
                            if(m_map[row][col] > index)
                            {
                                m_map[row][col]--;
                            }
                        }
                    }
                    m_map[r][c - 1] = 0;
                    isEliminate = true;
                    for (int col = c - 2; col >= 0; --col) //消除块后再移动
                    {
                        if (m_map[r][col] > 0 && m_map[r][col + 1] == 0) 
                        {
                            m_map[r][col + 1] = m_map[r][col];
                            m_map[r][col] = 0;
                            m_vec.at(m_map[r][col + 1] - 1)->moveTo(r, col + 1);
                        }
                    }
                }
            }
        }
    }
}

void  StartGame::newTile(float dt)
{
    auto tile = NumTile::create();
    int free_num =16 - m_vec.size();
    if (free_num == 0)
    {
        return;
    }
    int num = rand()%free_num;
    int row = -1;
    int col = -1;
    int count = 0;
    bool loop = false;
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL; ++c)
        {
            if (m_map[r][c] == 0) //记录空白数量
            {
                count++;
            }
            if (count == num+1) 
            {
                row = r;
                col = c;
                loop = true;
                break;
            }
        }
        if (loop)
        {
            break;
        }
    }
    CCASSERT(row != -1,"");
    CCASSERT(col != -1,"");
    tile->showAt(row, col);
    bg->addChild(tile);
    m_vec.pushBack(tile);
    m_map[row][col] = m_vec.getIndex(tile)+1;
}
void StartGame::restart()
{
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL; c++)
        {
            if (m_map[r][c] > 0)
            {
                int index = m_map[r][c];
                m_vec.at(m_map[r][c]-1)->removeFromParent();
                m_vec.erase(m_map[r][c]-1);
                for (int row = 0; row < ROW; ++row)
                {
                    for (int col = 0; col < COL; ++col)
                    {
                        if(m_map[row][col] > index)
                        {
                            m_map[row][col]--;
                        }
                    }
                }
                m_map[r][c] = 0;
            }
        }
    }
    newTile(0.1f);
    score = 0;
    score_label->setString(StringUtils::format("%d",score));
}
bool StartGame::canMove()
{
    bool b_move = false;
    for (int r = 0; r < ROW; ++r)
    {
        for (int c = 0; c < COL - 1; ++c)
        {
            if (m_vec.at(m_map[r][c]-1)->m_number == 
                m_vec.at(m_map[r][c + 1]-1)->m_number) 
            {
                b_move = true;
            }
        }
    }
    for (int c = 0; c < COL; ++c)
    {
        for (int r = 0; r < ROW - 1; ++r)
        {
            if (m_vec.at(m_map[r][c]-1)->m_number == 
                m_vec.at(m_map[r + 1][c]-1)->m_number) 
            {
                b_move = true;
            }
        }
    }
    return b_move;
}

void StartGame::stopCallBack()
{
    
}

void StartGame::stopGame()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif
    
    Director::getInstance()->end();
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
//111